package com.wmapp.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wmapp.utils.Pserver;
import com.wmapp.utils.SimpleStringUtils;

public class AppListener implements ServletContextListener{
	private static Log log = LogFactory.getLog(AppListener.class);
	
	@Override
	public void contextDestroyed(ServletContextEvent event) {}
	
	@Override
	public void contextInitialized(ServletContextEvent event) {
		String realPath=event.getServletContext().getRealPath("/");
		//String[] vals=realPath.split(SimpleStringUtils.join("\\",File.separator));
		//String ctxPath=SimpleStringUtils.join("/",vals[vals.length-1]);
		String ctxPath=event.getServletContext().getInitParameter("ctxPath");
		
		if(ctxPath==null||ctxPath.equals("/")){
			ctxPath="";
		}
		log.debug(SimpleStringUtils.join("web app real path is ",realPath));
		
		// register pserver path
		Pserver.getInstance().setPath(SimpleStringUtils.join(realPath,"/WEB-INF/pserver"));
		// register context path
		Pserver.getInstance().setContextPath(ctxPath);
		log.debug(SimpleStringUtils.join("web app context path is ",ctxPath));
	}
}
